package com.zoweunion.mechanic.controller.base;

import com.github.pagehelper.PageInfo;
import com.zoweunion.mechanic.controller.BaseController;
import com.zoweunion.mechanic.model.ResponseBean;
import com.zoweunion.mechanic.model.User;
import com.zoweunion.mechanic.service.base.EngineLockCarService;
import com.zoweunion.mechanic.service.base.LockCarService;
import com.zoweunion.mechanic.util.MyException;
import com.zoweunion.mechanic.util.constants.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/engineLockCar")
public class EngineLockCarController extends BaseController {

    @Autowired
    private EngineLockCarService engineLockCarService;
    @Autowired
    private LockCarService lockCarService;


    /**
     *服务商按省分组
     *
    */
    @PostMapping("/serverByProvince")
    public ResponseBean serverByProvince(@RequestBody Map<String, Object> reqMap, HttpServletRequest request) throws MyException {
        logBefore(logger, "服务商按省分组");
        User currentUser = getCurrentUser(request);
        reqMap.put("userId", currentUser.getId());
        Map<Integer, List<Map<String, Object>>> serverByProvinceMap = new HashMap<>();
        serverByProvinceMap = engineLockCarService.getServerByProvinceMap(reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS,"操作成功", serverByProvinceMap);
    }

    /**
     * 车主列表展示 ((分页,条件查询)
     * @param reqMap 分页条件 查询条件(车主姓名,电话)
     * @return 车主信息 包含已拥有车辆数
     */
    @PostMapping("/list")
    public ResponseBean list(@RequestBody Map<String, Object> reqMap) {
        logBefore(logger, "车主管理_列表展示");
        PageInfo<Map<String, Object>> resultList = engineLockCarService.getPageQuery(reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",resultList);
    }

    /**
     * 车主名下车辆列表展示
     * @param reqMap 查询条件(车主姓名,电话，物联编号，制造编号)
     * @return 车主信息
     */
    @PostMapping("/carListByOwner")
    public ResponseBean carListByOwner(@RequestBody Map<String, Object> reqMap) {
        logBefore(logger, "车主管理_列表展示");
        List<Map<String, Object>> resultList = engineLockCarService.carListByOwner(reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",resultList);
    }

    /**
     * 服务商被锁车计数
     *
     * @return
     */
    @PostMapping("lockCarCount")
    public ResponseBean lockCarCount(@RequestBody Map<String, Object> reqMap) {
        logBefore(logger, "被锁车计数");
        Integer lockCarCount = engineLockCarService.getLockCarCount(reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", lockCarCount);
    }

    /**
     * 服务商车辆计数
     *
     * @return
     */
    @PostMapping("engineCarCount")
    public ResponseBean engineCarCount(@RequestBody Map<String, Object> reqMap) {
        logBefore(logger, "生产商车辆计数");
        Integer engineCarCount = engineLockCarService.getEngineCarCount(reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", engineCarCount);
    }

    /**
     * 生产商被锁车计数
     *
     * @return
     */
    @PostMapping("lockCarCountEngine")
    public ResponseBean lockCarCountEngine(@RequestBody Map<String, Object> reqMap) {
        logBefore(logger, "被锁车计数");
        Integer lockCarCount = engineLockCarService.lockCarCountEngine(reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", lockCarCount);
    }

    /**
     * 生产商总车辆计数
     *
     * @return
     */
    @PostMapping("carCountEngine")
    public ResponseBean carCountEngine(@RequestBody Map<String, Object> reqMap) {
        logBefore(logger, "生产商车辆计数");
        Integer engineCarCount = engineLockCarService.carCountEngine(reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", engineCarCount);
    }

    /**
     * 车辆命令插入
     *
     * @return
     */
    @PostMapping("thingsIdCarOperation")
    public ResponseBean thingsIdCarOperation(@RequestBody Map<String, Object> reqMap, HttpServletRequest request) throws MyException {
        logBefore(logger, "车辆命令插入");
        User currentUser = getCurrentUser(request);
        reqMap.put("lock_user_id", currentUser.getId());
        reqMap.put("create_user", currentUser.getId());
        reqMap.put("update_user", currentUser.getId());

        engineLockCarService.thingsIdCarOperation(reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", true);
    }

    /**
     * 锁车记录查询
     *
     * @return
     */
    @PostMapping("thingsIdCarRecord")
    public ResponseBean thingsIdCarRecord(@RequestBody Map<String, Object> reqMap) {
        logBefore(logger, "锁车记录查询");
        List<Map<String, Object>> thingsIdCarRecord = lockCarService.thingsIdCarRecord(reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", thingsIdCarRecord);
    }
}
